<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
 <h1>spring-websocket-stomp</h1>
  <button id="btn-send">广播消息</button>
  <button id="btn-send2">精准投送消息</button>
  <button id="btn-send3">动态广播</button>
  <button id="btn-send4">动态精准投送</button>
</body>


  <!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery，所以必须放在前边) -->
    <script src="js/jquery/jquery-2.1.4.min.js"></script>
    <script src="js/socket/sockjs.min.js"></script>
	
    <script src="js/socket/stomp.min.js"></script>

<script>
	var url=getUrl();
	
	console.log(url);
	//new 
	    var  socket = new  SockJS('http://127.0.0.1:8080/endpointOyzc');   //WebSocket对应的地址
	    stompClient = Stomp.over(socket);
	    var headers={
	        username:'admin',
	        password:'admin'
	    };
	    stompClient.connect(headers,function (frame) {
	        //setConnected(true);
	       // console.log('Connected: ' + frame);
	        //订阅AAA频道
	        stompClient.subscribe('/topic/AAA', function (greeting) {
	             //后台传过来的json字符串转换为json对象 
	              //var obj=  JSON.parse(greeting.body);
	             //处理并显示数据 
				 console.log('/topic/AAA: ' + greeting);
	        });
	       //订阅BBB频道
	        stompClient.subscribe('/topic/BBB', function (greeting) {
	             //后台传过来的json字符串转换为json对象 
	              //var obj=  JSON.parse(greeting.body);
	             //处理并显示数据 
				  console.log('/topic/BBB: ' + greeting);
	        });
	    });
	 
	//获取项目路径
	 function getUrl() {
	     var strFullPath=window.document.location.href;
	     var strPath=window.document.location.pathname;
	     var pos=strFullPath.indexOf(strPath);
	     var prePath=strFullPath.substring(0,pos);
	     var postPath=strPath.substring(0,strPath.substr(1).indexOf('/')+1);
	     var basePath = prePath+postPath;
	     var url=basePath.replace("http","ws")+"//websocket/socketServer.action";
	     return url
	 }
</script>

</html>